<?php
session_start();
include '../import_admin.php';

if (!isset($_SESSION[SESSION_ROLE])){
    $_SESSION[SESSION_ROLE] = null;
}
$response_array = array();

// check contrller
if (! isset( $_REQUEST[PARAM_CONTROLLER]) || empty( $_REQUEST[PARAM_CONTROLLER])) {
    
    $response_array[STATUS] = STATUS_ERROR;
    $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_CONTROLLER;
    echo json_encode( $response_array);

} else 
    if (! isset( $_REQUEST[PARAM_ACTION]) || empty( $_REQUEST[PARAM_ACTION])) { // check action
        
        $response_array[STATUS] = STATUS_ERROR;
        $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_ACTION;
        echo json_encode( $response_array);
    
    } else {
        
        // get controller and action
        $controller = $_REQUEST[PARAM_CONTROLLER];
        $action = $_REQUEST[PARAM_ACTION];
        
        // check user permission
        if (! checkAccessPermision( $_SESSION[SESSION_ROLE], $controller, $action)) {
            
            $response_array[STATUS] = STATUS_DECLINED;
            $response_array[MESSAGE] = MESSAGE_ACCESS_DECLINED;
            echo json_encode( $response_array);
        
        } else {
            
            // swich controller
            switch ($controller) {
                case PVALUE_CONTROLLER_PAGE_WEBINFO : // PVALUE_CONTROLLER_PAGE_WEBINFO
                    $ctrl = new WebsiteController();
                    switch ($action) {
                        case PVALUE_ACTION_SUBMIT_FORM :
                            $response_array = $ctrl->submitForm();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_SORT :
                            $response_array = $ctrl->submitSort();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_ONE :
                            $response_array = $ctrl->submitDeleteOne();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_MANY :
                            $response_array = $ctrl->submitDelteMany();
                            echo json_encode( $response_array);
                            break;
                        
                        default :
                            $response_array[STATUS] = STATUS_ERROR;
                            $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_ACTION;
                            echo json_encode( $response_array);
                            break;
                    }
                    break;
                
                case PVALUE_CONTROLLER_PAGE_ORGANINFO :  // PVALUE_CONTROLLER_PAGE_ORGANINFO
                    $ctrl = new OrganInfoController();
                    switch ($action) {
                        case PVALUE_ACTION_SUBMIT_FORM :
                            $response_array = $ctrl->submitForm();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_SORT :
                            $response_array = $ctrl->submitSort();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_ONE :
                            $response_array = $ctrl->submitDeleteOne();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_MANY :
                            $response_array = $ctrl->submitDelteMany();
                            echo json_encode( $response_array);
                            break;
                        
                        default :
                            $response_array[STATUS] = STATUS_ERROR;
                            $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_ACTION;
                            echo json_encode( $response_array);
                            break;
                    }
                    break;
                
                case PVALUE_CONTROLLER_PAGE_USERS :  // PVALUE_CONTROLLER_PAGE_USERS
                    $ctrl = new UserController();
                    switch ($action) {
                        case PVALUE_ACTION_SUBMIT_LOGIN :
                            $response_array = $ctrl->userLogin();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_RESETPASSWORD :
                            $response_array = $ctrl->submitResetPassword();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_PROFILE :
                            $response_array = $ctrl->submitProfile();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_CHANGEPASSWORD :
                            $response_array = $ctrl->submitChangePassword();
                            echo json_encode( $response_array);
                            break;
                        
                        default :
                            $response_array[STATUS] = STATUS_ERROR;
                            $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_ACTION;
                            echo json_encode( $response_array);
                            break;
                    }
                    break;
                
                case PVALUE_CONTROLLER_PAGE_ACCOUNT : // PVALUE_CONTROLLER_PAGE_ACCOUNT
                    $ctrl = new AccountController();
                    switch ($action) {
                        case PVALUE_ACTION_SUBMIT_FORM :
                            $response_array = $ctrl->submitForm();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_SORT :
                            $response_array = $ctrl->submitSort();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_ONE :
                            $response_array = $ctrl->submitDeleteOne();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_MANY :
                            $response_array = $ctrl->submitDelteMany();
                            echo json_encode( $response_array);
                            break;
                        
                        default :
                            $response_array[STATUS] = STATUS_ERROR;
                            $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_ACTION;
                            echo json_encode( $response_array);
                            break;
                    }
                    break;
                
                case PVALUE_CONTROLLER_PAGE_PAGESUPPORT : // PVALUE_CONTROLLER_PAGE_PAGESUPPORT
                    $ctrl = new SupportController();
                    switch ($action) {
                        case PVALUE_ACTION_SUBMIT_FORM :
                            $response_array = $ctrl->submitForm();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_SORT :
                            $response_array = $ctrl->submitSort();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_ONE :
                            $response_array = $ctrl->submitDeleteOne();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_MANY :
                            $response_array = $ctrl->submitDelteMany();
                            echo json_encode( $response_array);
                            break;
                        
                        default :
                            $response_array[STATUS] = STATUS_ERROR;
                            $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_ACTION;
                            echo json_encode( $response_array);
                            break;
                    }
                    break;
                
                case PVALUE_CONTROLLER_PAGE_PAGEIMAGES : // PVALUE_CONTROLLER_PAGE_PAGEIMAGES
                    $ctrl = new PageImageController();
                    switch ($action) {
                        case PVALUE_ACTION_SUBMIT_FORM :
                            $response_array = $ctrl->submitForm();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_SORT :
                            $response_array = $ctrl->submitSort();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_ONE :
                            $response_array = $ctrl->submitDeleteOne();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_MANY :
                            $response_array = $ctrl->submitDelteMany();
                            echo json_encode( $response_array);
                            break;
                        
                        default :
                            $response_array[STATUS] = STATUS_ERROR;
                            $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_ACTION;
                            echo json_encode( $response_array);
                            break;
                    }
                    break;
                
                case PVALUE_CONTROLLER_PAGE_PAGEPOPUP : // PVALUE_CONTROLLER_PAGE_PAGEPOPUP
                    $ctrl = new PopupController();
                    switch ($action) {
                        case PVALUE_ACTION_SUBMIT_FORM :
                            $response_array = $ctrl->submitForm();
                            echo json_encode( $response_array);
                            break;
                        
                        default :
                            $response_array[STATUS] = STATUS_ERROR;
                            $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_ACTION;
                            echo json_encode( $response_array);
                            break;
                    }
                    break;
                
                case PVALUE_CONTROLLER_VIEW_CATEGORIES : // PVALUE_CONTROLLER_VIEW_CATEGORIES
                    $ctrl = new CategoryController();
                    switch ($action) {
                        case PVALUE_ACTION_SUBMIT_FORM :
                            $response_array = $ctrl->submitForm();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_SORT :
                            $response_array = $ctrl->submitSort();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_ONE :
                            $response_array = $ctrl->submitDeleteOne();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_MANY :
                            $response_array = $ctrl->submitDelteMany();
                            echo json_encode( $response_array);
                            break;
                        
                        default :
                            $response_array[STATUS] = STATUS_ERROR;
                            $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_ACTION;
                            echo json_encode( $response_array);
                            break;
                    }
                    break;
                
                case PVALUE_CONTROLLER_VIEW_ARTICLES : // PVALUE_CONTROLLER_VIEW_ARTICLES
                    $ctrl = new ArticleController();
                    switch ($action) {
                        case PVALUE_ACTION_SUBMIT_FORM :
                            $response_array = $ctrl->submitForm();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_SORT :
                            $response_array = $ctrl->submitSort();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_ONE :
                            $response_array = $ctrl->submitDeleteOne();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_MANY :
                            $response_array = $ctrl->submitDeleteMany();
                            echo json_encode( $response_array);
                            break;
                        
                        default :
                            $response_array[STATUS] = STATUS_ERROR;
                            $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_ACTION;
                            echo json_encode( $response_array);
                            break;
                    }
                    break;
                
                case PVALUE_CONTROLLER_VIEW_PRODUCTS : // PVALUE_CONTROLLER_VIEW_PRODUCTS
                    $ctrl = new ProductController();
                    switch ($action) {
                        case PVALUE_ACTION_SUBMIT_FORM :
                            $response_array = $ctrl->submitForm();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_SORT :
                            $response_array = $ctrl->submitSort();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_ONE :
                            $response_array = $ctrl->submitDeleteOne();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_MANY :
                            $response_array = $ctrl->submitDeleteMany();
                            echo json_encode( $response_array);
                            break;
                        
                        default :
                            $response_array[STATUS] = STATUS_ERROR;
                            $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_ACTION;
                            echo json_encode( $response_array);
                            break;
                    }
                    break;
                
                case PVALUE_CONTROLLER_PAGE_PRODUCTIMAGES : // PVALUE_CONTROLLER_PAGE_PRODUCTIMAGES
                    $ctrl = new ProductImageController();
                    switch ($action) {
                        case PVALUE_ACTION_SUBMIT_FORM :
                            $response_array = $ctrl->submitForm();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_SORT :
                            $response_array = $ctrl->submitSort();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_ONE :
                            $response_array = $ctrl->submitDeleteOne();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_MANY :
                            $response_array = $ctrl->submitDeleteMany();
                            echo json_encode( $response_array);
                            break;
                        
                        default :
                            $response_array[STATUS] = STATUS_ERROR;
                            $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_ACTION;
                            echo json_encode( $response_array);
                            break;
                    }
                    break;
                
                case PVALUE_CONTROLLER_VIEW_ALBUMS : // PVALUE_CONTROLLER_VIEW_ALBUMS
                    $ctrl = new AlbumController();
                    switch ($action) {
                        case PVALUE_ACTION_SUBMIT_FORM :
                            $response_array = $ctrl->submitForm();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_SORT :
                            $response_array = $ctrl->submitSort();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_ONE :
                            $response_array = $ctrl->submitDeleteOne();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_MANY :
                            $response_array = $ctrl->submitDeleteMany();
                            echo json_encode( $response_array);
                            break;
                        
                        default :
                            $response_array[STATUS] = STATUS_ERROR;
                            $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_ACTION;
                            echo json_encode( $response_array);
                            break;
                    }
                    break;
                
                case PVALUE_CONTROLLER_VIEW_IMAGES : // PVALUE_CONTROLLER_VIEW_IMAGES
                    $ctrl = new ImagesController();
                    switch ($action) {
                        case PVALUE_ACTION_SUBMIT_FORM :
                            $response_array = $ctrl->submitForm();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_SORT :
                            $response_array = $ctrl->submitSort();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_ONE :
                            $response_array = $ctrl->submitDeleteOne();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_MANY :
                            $response_array = $ctrl->submitDelteMany();
                            echo json_encode( $response_array);
                            break;
                        
                        default :
                            $response_array[STATUS] = STATUS_ERROR;
                            $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_ACTION;
                            echo json_encode( $response_array);
                            break;
                    }
                    break;
                
                case PVALUE_CONTROLLER_VIEW_FILES : // PVALUE_CONTROLLER_VIEW_VIDEOS
                    $ctrl = new FileController();
                    switch ($action) {
                        case PVALUE_ACTION_SUBMIT_FORM :
                            $response_array = $ctrl->submitForm();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_SORT :
                            $response_array = $ctrl->submitSort();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_ONE :
                            $response_array = $ctrl->submitDeleteOne();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_MANY :
                            $response_array = $ctrl->submitDeleteMany();
                            echo json_encode( $response_array);
                            break;
                        
                        default :
                            $response_array[STATUS] = STATUS_ERROR;
                            $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_ACTION;
                            echo json_encode( $response_array);
                            break;
                    }
                    break;
                
                case PVALUE_CONTROLLER_VIEW_VIDEOS : // PVALUE_CONTROLLER_VIEW_VIDEOS
                    $ctrl = new VideosController();
                    switch ($action) {
                        case PVALUE_ACTION_SUBMIT_FORM :
                            $response_array = $ctrl->submitForm();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_SORT :
                            $response_array = $ctrl->submitSort();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_ONE :
                            $response_array = $ctrl->submitDeleteOne();
                            echo json_encode( $response_array);
                            break;
                        
                        case PVALUE_ACTION_SUBMIT_DELETE_MANY :
                            $response_array = $ctrl->submitDeleteMany();
                            echo json_encode( $response_array);
                            break;
                        
                        default :
                            $response_array[STATUS] = STATUS_ERROR;
                            $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_ACTION;
                            echo json_encode( $response_array);
                            break;
                    }
                    break;
                
                default :
                    $response_array[STATUS] = STATUS_ERROR;
                    $response_array[MESSAGE] = MESSAGE_NOT_DEFINE_CONTROLLER;
                    echo json_encode( $response_array);
                    break;
            }
        }
    
    }

?>